package io.renren.modules.sys.web_api.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.PinYin4jUtils;
import io.renren.modules.sys.back.entity.BtPingpaiInfoEntity;
import io.renren.modules.sys.back.entity.BtRenwuInfoEntity;
import io.renren.modules.sys.back.service.BtPingpaiInfoService;
import io.renren.modules.sys.back.service.BtRenwuInfoService;
import io.renren.modules.sys.web_api.service.PinPaiPageService;
import io.renren.modules.sys.web_api.service.RenWuPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("pinPaiPageService")
public class PinPaiPageServiceImpl implements PinPaiPageService{
    @Autowired
    private BtPingpaiInfoService btPingpaiInfoService;
    @Override
    public void SetFirstLeb(){
        List<BtPingpaiInfoEntity> rwblist = btPingpaiInfoService.selectList(new EntityWrapper<BtPingpaiInfoEntity>());
        for(BtPingpaiInfoEntity sb:rwblist){
            if(sb.getFirstl() == null){
            String[] headArray = PinYin4jUtils.getHeadByString(sb.getName().substring(0,1));// 获得每个汉字拼音首字母
            List<String> list = Arrays.asList(headArray);
            sb.setFirstl(list.get(0));
            btPingpaiInfoService.insertOrUpdate(sb);}
        }
    }
    @Override
    public List<Map<String,Object>> getPinPaiList(){
        List<Map<String,Object>> backdata = new ArrayList<>();
        List<Map<String,Object>> lblist = btPingpaiInfoService.getLebSortMap();
        for(int i=0;i<lblist.size();i++){
            Map<String,Object> shuju = new HashMap<>();
            Collection<String> c = new ArrayList();
            c.add("llcs");
            List<BtPingpaiInfoEntity> rwblist = btPingpaiInfoService.selectList(new EntityWrapper<BtPingpaiInfoEntity>().eq("firstl",lblist.get(i).get("firstl")).orderDesc(c));
            List<Map<String,Object>> beanlist = new ArrayList<>();
            for(BtPingpaiInfoEntity sb :rwblist){
                Map<String,Object> tem = new HashMap<>();
                tem.put("name",sb.getName());
                tem.put("img",sb.getImg());
                tem.put("link","/api/pinpai/detail/"+sb.getId());
                beanlist.add(tem);
            }
            shuju.put("zimu",lblist.get(i).get("firstl"));
            shuju.put("content",beanlist);
            backdata.add(shuju);
        }
        return backdata ;
    }
    @Override
    public List<Map<String,Object>> getPinPaituijan(){
        List<Map<String,Object>> backdata = new ArrayList<>();
        Collection<String> c = new ArrayList();
        c.add("llcs");
        List<BtPingpaiInfoEntity> rwblist = btPingpaiInfoService.selectList(new EntityWrapper<BtPingpaiInfoEntity>().orderDesc(c));

        for(BtPingpaiInfoEntity sb :rwblist){
            Map<String,Object> tem = new HashMap<>();
            tem.put("name",sb.getName());
            tem.put("img",sb.getImg());
            tem.put("link","/api/renwu/detail/"+sb.getId());
            backdata.add(tem);
        }
        return backdata ;
    }
    @Override
    public Map<String,Object> getPinPaiDatailInfo(Long id){
        Map<String,Object> backdata = new HashMap<>();
        BtPingpaiInfoEntity rwbean = btPingpaiInfoService.selectById(id);
        backdata.put("naverji","品牌");
        backdata.put("naverjilink","/api/pinpai/first");
        backdata.put("navwuji",rwbean.getName());
        Map<String,Object> subcontent = null;
        //概述
        subcontent = new HashMap<>();
        subcontent.put("name","简介");
        subcontent.put("content",rwbean.getSummer());
        backdata.put("gaishu",subcontent);
        //工作原理
        subcontent = new HashMap<>();

        if(rwbean.getContent()!=null){
            subcontent.put("name","内容");
            subcontent.put("content",rwbean.getContent());
            backdata.put("gongzhuyuanli",subcontent);
        }
        rwbean.setLlcs(rwbean.getLlcs()+1);
        btPingpaiInfoService.insertOrUpdate(rwbean);
        return backdata;
    }
}
